(function(w , d){

    var scrollWarpper = function(el , options) {
        var me = this, i;

        me.options = {
            onBeforUpDownFlat: true, //上下开关
			touchForbidFlat:true, //禁止连续拖动
			touchUpperFlat:true, //上拉开关
			touchDownFlat:true, //下拉开关
			pullUpClassName:'pullUp', //上拉元素的class
			pullDownClassName:'pullDown', //下拉元素的class
			
            x: 0,  //元素的x轴
            y: 0,  //元素的y轴
            touchX:0, //鼠标的x轴
            touchY:0, //鼠标的y轴
            startY: 0, //鼠标开始触摸的y轴
            startX: 0, //鼠标开始触摸的x轴

            scrollTag:'scroller', //需要滑动的元素
            pulldownOffsetHeight:40, //下拉元素的高度
            pullupOffsetHeight:40, //上拉元素的高度

            scrollWarpperTop:0, //元素的top值

            onscrollBar:false,  //文档是否显示滚动条
            touchmoveiniz:'下拉刷新',
            touchmovetxt : '松开手开始刷新',
            touchenddowntxt :'刷新中...',
            touchendupiniz:'上拉加载更多',
			touchendtxt:'松开手开始加载',
            touchenduptxt:'加载中...',
            touchendupnulltxt : '没有更多数据了',

            toRoll : 0,
            docHeight:0,
            soAreaHeight:0,
            surplusHeight:0,

            touchmoveDownFn : function(args){}, //刷新回调
            touchmoveUpFn:function(args){}  //加载回调
        }
		
        for (i in options) {
            me.options[i] = options[i];
        }

        me.options.transY = this.options.pulldownOffsetHeight;

        this.moveScrollOnOff = false;
        this.warpper = typeof el == 'object' ? el : d.getElementById(el);
        this.scrollWarpper = typeof me.options.scrollTag == 'object' ? me.options.scrollTag : d.getElementById(me.options.scrollTag);
		
		this.pullUp = this.warpper.querySelector('.'+me.options.pullUpClassName);
		this.pullDown = this.warpper.querySelector('.'+me.options.pullDownClassName);
		me.options.pulldownOffsetHeight = parseInt(this.pullDown.offsetHeight);
		me.options.pullupOffsetHeight = parseInt(this.pullUp.offsetHeight);
		
		//this.pullUp.style.top = (this.scrollWarpper.offsetHeight - me.options.pullupOffsetHeight)+'px';

        me.options.toRoll = $(document).scrollTop();
        me.options.docHeight = $(document).height();
        me.options.soAreaHeight = $(window).height();
        me.options.surplusHeight = me.options.docHeight - me.options.toRoll - me.options.soAreaHeight;

        //!this.options.onscrollBar ? this.warpper.style.overflowY ="hidden" : this.warpper.style.overflowY = 'auto';

        this.START_EV = 'touchstart',
        this.MOVE_EV = 'touchmove',
        this.END_EV = 'touchend',

        //绑定事件
        me._bind(this.START_EV);
        me._bind(this.END_EV);
        me._bind(this.MOVE_EV);
    }

    scrollWarpper.prototype={
        handleEvent: function (e) {
            var me = this;
            switch(e.type) {
                case this.MOVE_EV: me._move(e); break;
                case this.END_EV:me._end(e);break;
                case this.START_EV:me._start(e);break;
            }
        },
        _bind: function (type, el, bubble) {
            (el || this.warpper).addEventListener(type, this, !!bubble);
        },
        _start:function(event){
            var e = e || event;
            if(e.targetTouches.length > 0){
                var touch = e.targetTouches[0];
                this.options.startY = touch.pageY;
                this.options.startX = touch.pageX;
            }
			
        },
        _move:function(event){
			
			var e = event || window.event;
		
			if(!this.options.touchForbidFlat){
				return;
			}
            var touch = event.targetTouches[0];
            this.options.touchX = touch.pageX;
            this.options.touchY = touch.pageY;
            this.options.x = this.options.touchX;
			
			//如果上拉下拉状态为打开才去执行 刷新和加载
			if(this.options.onBeforUpDownFlat){
				//如果卷去的高度小于等于1
			   if(this.warpper.scrollTop <= 1 && this.options.touchDownFlat){
				   //如果开始触摸小于结束触摸坐标 往下拉 说明往上拉到顶部了
					if(this.options.startY  < this.options.touchY) {
						this.options.scrollWarpperTop = this.options.touchY - this.options.startY;
						if (this.options.scrollWarpperTop >= this.options.pulldownOffsetHeight) {
							this.pullDown.innerText = this.options.touchmovetxt;
						}
						
						this.refresh();
					}
			   }
				
				//如果数据容器高度 - 父容器卷去高度 - 父容器固定高度 <= 0  说明是往上拉到底部了
				if(this.scrollWarpper.offsetHeight - this.warpper.scrollTop - this.warpper.offsetHeight <= 0 && this.options.touchUpperFlat){
					//再次验证是否是往上拉
					if(this.options.startY > this.options.touchY){
						//往上滑动的值
						this.options.scrollWarpperTop = parseInt('-'+(this.options.startY - this.options.touchY));
						//如果滑动值大于上拉提示的容器高度
						if(Math.abs(this.options.scrollWarpperTop) >= this.options.pullupOffsetHeight) {
							this.pullUp.innerText = this.options.touchendtxt;
						}
						this.refresh();
					}
				}
			}
        },
        _end:function(){
			if(!this.options.touchForbidFlat){
				return;
			}
			if(this.options.onBeforUpDownFlat){
				
				var swrTop = parseInt(this.scrollWarpper.style.top.replace(/[a-z]/g,''));
				if(swrTop && swrTop > 0 && this.options.touchDownFlat) {
						if (this.options.scrollWarpperTop >= this.options.pulldownOffsetHeight) {
							this.options.scrollWarpperTop = this.options.pulldownOffsetHeight;
							this.pullDown.innerText = this.options.touchenddowntxt;
							this.refresh();
							this.options.touchForbidFlat = false;
							this.options.touchmoveDownFn(this);
						} else {
							this.options.scrollWarpperTop = 0;
							this.refresh();
						}
				}
				
				if(swrTop && swrTop < 0 && this.options.touchUpperFlat){
					if(Math.abs(this.options.scrollWarpperTop) >= this.options.pullupOffsetHeight) {
						 this.options.scrollWarpperTop = parseInt('-'+this.options.pullupOffsetHeight);
						 this.pullUp.innerText = this.options.touchenduptxt;
						 this.refresh();
						 this.options.touchForbidFlat = false;
						 this.options.touchmoveUpFn(this);
					}else{
						this.options.scrollWarpperTop = 0;
						this.refresh();
					}
				}
			}
			
        },
        refresh:function(){
            this.scrollWarpper.style.top = this.options.scrollWarpperTop+'px';
			this.pullUp.style.top = (this.scrollWarpper.offsetHeight - this.options.pullupOffsetHeight)+'px';
			this.options.touchForbidFlat = true;
        }
    }
    w._Scroll = {
        init:function(el,options){
            if(el) {
                this.init.prototype = new scrollWarpper(el, options);
            }
        }
    }
})(window ,document);